package com.newhero.app.report.web.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.newhero.product.common.controller.BaseController;
import com.newhero.product.common.mybatis.mapper.EntityWrapper;
import com.newhero.product.common.util.StringUtils;
import com.newhero.product.common.vo.Json;
import com.newhero.product.common.mybatis.plugins.Page;
import com.newhero.app.report.persistence.entity.Feedback;
import com.newhero.app.report.service.IFeedbackService;
import com.newhero.product.util.ExportUtil;

 /**
 * @ClassName: FeedbackController
 * @Description: 访问控制类
 * @author zlf
 * @date 2018-09-07
 */
@Controller
@RequestMapping("/feedback")
public class FeedbackController extends BaseController {
    
    @Autowired 
    private IFeedbackService feedbackServiceImpl;
    																				
    /**
     * 列表页面
     * @return
     */    
	@RequestMapping(value = "/feedbackList", method ={RequestMethod.GET,RequestMethod.POST })
	public String feedbackList(
			@RequestParam(value = "page", defaultValue = "1", required = true) int page,// 页号
			@RequestParam(value = "rows", defaultValue = "" + _ROWS_PER_PAGE, required = true) int rows,// 每页显示的记录条数
			@RequestParam(value = "orderField", defaultValue = "feedback_id", required = true) String orderField,
			@RequestParam(value = "isAsc", defaultValue = "0", required = true) int isAsc,//1-代表true;0-代表false
			Feedback feedback,
			HttpServletRequest request, HttpServletResponse response, ModelMap model) {
		Page<Feedback> p = new Page<Feedback>();
		p.setCurrent(page);
		p.setSize(rows);
		p.setOrderByField(orderField);
		if(isAsc==0){
			p.setAsc(false);
		}else{
			p.setAsc(true);
		}
		
		EntityWrapper<Feedback> entityWrapper = new EntityWrapper<Feedback>();
		
			
    	if(StringUtils.isNotBlank(feedback.getDiscloseId())){
			entityWrapper.like("disclose_id", feedback.getDiscloseId());
		}
		model.addAttribute("discloseId", feedback.getDiscloseId());
		
			
    	if(StringUtils.isNotBlank(feedback.getFeedbackName())){
			entityWrapper.like("feedback_name", feedback.getFeedbackName());
		}
		model.addAttribute("feedbackName", feedback.getFeedbackName());
		
			
    	if(StringUtils.isNotBlank(feedback.getTelephone())){
			entityWrapper.like("telephone", feedback.getTelephone());
		}
		model.addAttribute("telephone", feedback.getTelephone());
			
		
	
		p = feedbackServiceImpl.selectPage(p, entityWrapper);
		model.addAttribute("data", p);
		model.addAttribute("orderField", orderField);
		model.addAttribute("isAsc", isAsc);
		
		return "/report/feedback/feedbackList";
	}
	
    /**
     * 表单页面
     * @return
     */
	@RequestMapping(value = "/feedbackForm", method = RequestMethod.GET)
	public String feedbackForm(String feedbackId, HttpServletRequest request) {
		if (feedbackId != null && !"".equals(feedbackId)) {
			request.setAttribute("feedbackId", feedbackId);
			request.setAttribute("formData",feedbackServiceImpl.selectById(feedbackId));
		} else {
			request.setAttribute("feedbackId", "");
			request.setAttribute("formData", new HashMap<String, Object>());
		}
		return "/report/feedback/feedbackForm";
	}
    
    /**
     * 表单保存
     * @return
     */
	@RequestMapping(value = "/feedbackSave", method = RequestMethod.POST)
	public String feedbackSave(
			String feedbackId, Feedback feedback, 
			HttpServletRequest request,HttpServletResponse response, ModelMap model) {
		try {
			if (!StringUtils.isBlank(feedbackId)) {
				model.addAttribute("flag", "数据修改成功！");
				feedbackServiceImpl.updateById(feedback);
			} else {
				model.addAttribute("flag", "数据添加成功！");
				feedbackServiceImpl.insert(feedback);
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e);
		}
		return "redirect:feedbackList";
	}

    /**
     * 删除
     * @param ids
     * @return
     */
	@RequestMapping(value = "/feedbackDelete", method = { RequestMethod.POST, RequestMethod.GET })
	public String feedbackDelete(
			@RequestParam(value = "ids", required = true) String ids,
			HttpServletRequest request, HttpServletResponse response, ModelMap model) {
		String[] idArr = ids.split(",");
		feedbackServiceImpl.deleteBatchIds(Arrays.asList(idArr));
		model.addAttribute("flag", "数据删除成功！");
		return "redirect:feedbackList";
	}     
    
    
    @ResponseBody
	@RequestMapping(value = "/feedbackAjax", method = RequestMethod.POST)
	public Json feedbackAjax(String feedbackId) {
		Feedback feedback = feedbackServiceImpl.selectById(feedbackId);
		Json json = new Json();
		json.setSuccess(true);
		json.setObj(feedback);
		return json;
	}
	
	@RequestMapping(value = "/feedbackExportExcel", method = { RequestMethod.GET, RequestMethod.POST })
	public void feedbackExportExcel(
			Feedback feedback,
			HttpServletRequest request, HttpServletResponse response, ModelMap model) {
		
		EntityWrapper<Feedback> entityWrapper = new EntityWrapper<Feedback>();
		
	    if(StringUtils.isNotBlank(feedback.getFeedbackId())){
			entityWrapper.like("feedback_id", feedback.getFeedbackId());
		}
		model.addAttribute("feedbackId", feedback.getFeedbackId());
								
		List<Feedback> resultList = feedbackServiceImpl.selectList(entityWrapper);
		
		ExportUtil.export(request, response, resultList,"导出Excel","feedback", 0, 2, 0, 16);
	}
}
